package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcde.luowowo.website.util.CookieUtil;
import cn.wolfcde.luowowo.website.util.UploadUtil;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.domain.AjaxResult;
import cn.wolfcode.luowowo.common.key.RedisKey;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("setting")
public class SettingController {

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IDestinationService destinationService;

    //用户信息设置页面
    @RequestMapping("")
    public  String test(Model model,@RedisParameter UserInfo userInfo){
        model.addAttribute("user",userInfo);
        return "/mine/setting";
    }


    @RequestMapping("/home")
    public String home(Model model, @RedisParameter UserInfo userInfo){
        return "mine/homepage";
    }


    //头像页面
    @RequestMapping("/avatar")
    //@LoginRequired
    public String avater(Model model, @RedisParameter UserInfo userInfo) {
        model.addAttribute("user", userInfo);
        return "mine/avatar";
    }


    //头像上存更改
    @Value("${file.path}")
    private String filePath;

    //头像上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(Model model,MultipartFile pic, @RedisParameter UserInfo userInfo, HttpServletRequest request, HttpServletResponse response){
        //获取到上存图片的路径
        String upload = UploadUtil.upload(pic,filePath);
        StringBuilder sb = new StringBuilder(20);
        sb.append("/").append(upload);

        //通过id查询到该用户
        Long uid = userInfo.getId();
        userInfo.setHeadImgUrl(sb.toString());
        //更新用户头像
        userInfoService.updateHeadImgUrl(uid,sb.toString());
        UserInfo mysqlUser = userInfoService.selectUserById(uid);
        //更新redis中的数据
        String token = CookieUtil.getToken(request, response);
        userInfoRedisService.updateUserInfo(token, JSON.toJSONString(mysqlUser));
        UserInfo redisUser = userInfoRedisService.getLoginUserInfoByToken(token);
        //String s = RedisKey.LOGIN_USERINFO_PREFIX.appendKey(token);
        model.addAttribute("userInfo",redisUser);

        return  new AjaxResult().addObject(upload);
    }



    // 更新用户个人信息
    @RequestMapping("/saveUserInfo")
   // @RequireLogin
    public String saveUserInfo(UserInfo userInfo,HttpServletRequest request,Model model,HttpServletResponse response){
        //获取token
        String token = CookieUtil.getToken(request, response);
        //通过token获取userinfo
        UserInfo redisUserInfo = userInfoRedisService.getLoginUserInfoByToken(token);
        //页面没有传来的数据
        UserInfo user = new UserInfo();
        user.setId(redisUserInfo.getId());
        user.setHeadImgUrl(redisUserInfo.getHeadImgUrl());
        //页面传来的数据
        user.setCity(userInfo.getCity());
        user.setLevel(userInfo.getLevel());
        user.setInfo(userInfo.getInfo());
        user.setBirthday(userInfo.getBirthday());
        user.setGender(userInfo.getGender());
        user.setNickname(userInfo.getNickname());

        //更新redis中的键值
        userInfoRedisService.updateUserInfo(token, JSON.toJSONString(user));
        //更新mysql中的值
        userInfoService.updateUserInfo(user);
        //更新后获取
        UserInfo redisUserInfo2 = userInfoRedisService.getLoginUserInfoByToken(token);
        //更改MySQL中的userInfo
        model.addAttribute("user",redisUserInfo2);

        CookieUtil.addCookie(token,response);
        return "mine/setting";
    }


    //更改密码跳页面
    @RequestMapping("/changePassWord")
    public Object mobile(){
        //发送验证码
        //userInfoService.sendVerifyCode(userInfo.getPhone());
        return "/rpw.html";
    }

    //密码更改
    @RequestMapping("/mobile")
    @ResponseBody
    public AjaxResult updatepassword(String password,String rpassword1,String rpassword2,String smscode,String phone)  {
        userInfoService.updatepassword(password,rpassword1,rpassword2,smscode,phone);
        return new AjaxResult();
    }


    //根据手机号发送验证码
    @RequestMapping("/sendrpwVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone){
        AjaxResult ajaxResult = new AjaxResult();

        userInfoService.sendrpwVerifyCode(phone);

        return ajaxResult;

    }


    //设置更新邮箱邮箱
    @RequestMapping("/changeEmail")
    public  String securityemail(String email,Long id){
        return "/mine/email";
    }


    //用户注销申请  设置state为1
    @RequestMapping("/unregister")
    @ResponseBody
    public AjaxResult updateStateById(int state,Long id){
        userInfoService.updateStateById(1,id);
        return new AjaxResult();
    }


    //更改手机号
    @RequestMapping("/changePhone")
    @ResponseBody
    public AjaxResult updatePhoneById(String phone,Long id){
        userInfoService.updatePhoneById(phone,id);
        return new AjaxResult();
    }


    //绑定页面
    @RequestMapping("/bind")
    public String bind(Model model,@RedisParameter UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);
        return "mine/bind";
    }




    /*//我的窝设置
    @RequestMapping("/myhome")
    public String myhome(){
        return "mine/myhome";
    }*/





}
